package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_erp_log;

import cn.com.api.service.ApiT_erp_logService;

import cn.com.util.*;
import cn.com.util.*;


public class T_erp_logBaseController {

	private static Logger logger = Logger.getLogger(T_erp_logBaseController.class);
	
	@Autowired
	private ApiT_erp_logService apiT_erp_logService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_erp_log-list")
	public ModelAndView t_erp_log_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_erp_log_list.jsp");
		
		List<ApiT_erp_log> t_erp_logs = apiT_erp_logService.findT_erp_logs();
		mv.addObject("t_erp_logs", t_erp_logs);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_erp_log-add-pre")
	public ModelAndView t_erp_log_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_erp_log_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_erp_log t_erp_log = apiT_erp_logService.findT_erp_logById(Integer.valueOf(id));
			mv.addObject("t_erp_log",t_erp_log);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_erp_log-add")
	public void t_erp_log_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		//String id = request.getParameter("id");
		String goods_id = request.getParameter("goods_id");
		String products_id = request.getParameter("products_id");
		String action = request.getParameter("action");
		String order_id = request.getParameter("order_id");
		String invoke_time = request.getParameter("invoke_time");
		String status = request.getParameter("status");
		String remark = request.getParameter("remark");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_erp_log t_erp_log = new ApiT_erp_log();
            /*
		    t_erp_log.setId(id);
		    t_erp_log.setGoods_id(goods_id);
		    t_erp_log.setProducts_id(products_id);
		    t_erp_log.setAction(action);
		    t_erp_log.setOrder_id(order_id);
		    t_erp_log.setInvoke_time(invoke_time);
		    t_erp_log.setStatus(status);
		    t_erp_log.setRemark(remark);
	        */
			apiT_erp_logService.insertT_erp_log(t_erp_log);
		}
		//edit
		else{
			ApiT_erp_log t_erp_log = apiT_erp_logService.findT_erp_logById(Integer.valueOf(id));
	        /*
		    t_erp_log.setId(id);
		    t_erp_log.setGoods_id(goods_id);
		    t_erp_log.setProducts_id(products_id);
		    t_erp_log.setAction(action);
		    t_erp_log.setOrder_id(order_id);
		    t_erp_log.setInvoke_time(invoke_time);
		    t_erp_log.setStatus(status);
		    t_erp_log.setRemark(remark);
			*/
			apiT_erp_logService.updateT_erp_log(t_erp_log);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_erp_log-delete")
	public void t_erp_log_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_erp_logService.deleteT_erp_log(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



